package com.w.wx.service.impls;

import com.w.wx.domain.CommentsInfo;
import com.w.wx.domain.DTO.GoodsDeDTO;
import com.w.wx.domain.Goods;
import com.w.wx.domain.Notice;
import com.w.wx.domain.UserEr;
import com.w.wx.mapper.*;
import com.w.wx.mapper.DTOMapper;
import com.w.wx.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Timestamp;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;


@Service
@Slf4j
public class GoodServiceSImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private ImagesMapper imagesMapper;
    @Autowired
    private ShoppingCarMapper shoppingCarMapper;
    @Autowired
    private DTOMapper dtoMapper;
    @Autowired
    private UserErMapper userErMapper;
    @Autowired
    private LikedMapper likedMapper;
    @Autowired
    private CommentsInfoMapper commentsInfoMapper;
    @Autowired
    private CommentsReplyMapper commentsReplyMapper;

    @Override
    public int goodsIssue(Goods goodsInfo) {
        //物品信息
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        goodsInfo.setIssueTime(timestamp);
        goodsInfo.setState(0);
        if(goodsInfo.getGoodsPriceNew().compareTo(BigDecimal.ZERO)==0&&goodsInfo.getGoodsPriceOld().compareTo(BigDecimal.ZERO)==0){
            goodsInfo.setState(1);//未审核时状态,非免费物品
        }
        goodsInfo.setOpenSum(0);//刚发布浏览量必为0
        goodsInfo.setLikeSum(0);//点赞量也是0
       return goodsMapper.insert(goodsInfo);
    }

    @Override
    public int setGoodsState(Integer gid,Integer state,String reason) {
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        if (state==2&&reason==null){
            return 0;
        }
        if(state==2){
            Notice notice = new Notice();
            notice.setGid(gid);
            notice.setNoticeInfo(reason);
            notice.setSendTime(timestamp);
            noticeMapper.insert(notice);
        }
        imagesMapper.updateByGid(gid,state);
        return goodsMapper.updateGoodsState(state,timestamp,gid);
    }

    @Override
    public ArrayList<Goods> queryByUser(String flag) {

        return goodsMapper.selectByUser(flag);
    }

    @Override
    public ArrayList<Goods> getSimpleGoods(Integer state,int pageNum ,int pageSize) {
        pageNum *= pageSize;
        return goodsMapper.selectSimpleByState(state,pageNum,pageSize);
    }

    @Override
    public ArrayList<Goods> getMySimpleGoods(String openid,int pageNum ,int pageSize) {
        pageNum *= pageSize;
        return goodsMapper.selectMySimpleByOpenid(openid,1,pageNum,pageSize);
    }

    @Override
    public int deleteGoods(Integer gid) {
        //同时删除物品图片
        imagesMapper.deleteByGid(gid);
        //删除所有评论
        ArrayList<CommentsInfo> commentsInfos = commentsInfoMapper.selectAll(gid);
        Iterator<CommentsInfo> it = commentsInfos.iterator();
        while (it.hasNext()){
            CommentsInfo commentsInfo = it.next();
            commentsInfoMapper.deleteByPrimaryKey(commentsInfo.getInId());
            commentsReplyMapper.deleteByInid(commentsInfo.getInId());
        }

        return goodsMapper.deleteByPrimaryKey(gid);
    }

    @Override
    public Notice whyNotGo(Integer gid) {
        return noticeMapper.selectByGid(gid);
    }

    @Override
    public ArrayList<Goods> getSimFreeGoods(int pageNum ,int pageSize) {
        pageNum *= pageSize;
        return goodsMapper.selectSimFree(pageNum,pageSize);
    }


    @Override
    public GoodsDeDTO getDeGoodsFwl(Integer gid) {
        //简单的改变访问量
        int fwl = goodsMapper.selectFwlByGid(gid);
        goodsMapper.updateByPrimaryKey(gid ,fwl+1);
        Goods good =  goodsMapper.selectDetailByGid(gid);
        UserEr userEr = userErMapper.selectByOpenid(good.getOpenid());
        //查看当前用户点赞状态
        Integer i = likedMapper.selectForGetLikedState(gid, userEr.getOpenid(), 0);
        return dtoMapper.from(good,userEr,i);
    }

    @Override
    public GoodsDeDTO getDetailByGid(Integer gid) {
        Goods good =  goodsMapper.selectDetailByGid(gid);
        if (good==null){
            return null;
        }
        UserEr userEr = userErMapper.selectByOpenid(good.getOpenid());
        //查看当前用户点赞状态
        Integer i = likedMapper.selectForGetLikedState(gid, userEr.getOpenid(), 0);
        return dtoMapper.from(good,userEr,i);
    }

    @Override
    public int deleteCarGoods(Integer gid) {
        return shoppingCarMapper.deleteCarGoods(gid);
    }

    @Override
    public ArrayList<Goods> getMyCheckSimGoods(String openid) {
        ArrayList<Goods> goods =  goodsMapper.selectMyCheckSimGoods(openid);
        Iterator<Goods> it = goods.iterator();
        while(it.hasNext()){
            Goods good = it.next();
            //筛选掉免费物品信息
            if(good.getGoodsPriceNew().compareTo(BigDecimal.ZERO)==0&&good.getGoodsPriceOld().compareTo(BigDecimal.ZERO)==0){
                it.remove();
            }
        }
        return goods;
    }

    @Override
    public List<Goods> getAllGoodsInfo() {
        return goodsMapper.selectAll();
    }

    @Override
    public ArrayList<Goods> getAllSimpleGoods(String openid,Integer state, int pageNum, int pageSize) {
        pageNum *= pageSize;
        return goodsMapper.selectAllByState(openid,1,pageNum,pageSize);
    }

}
